package com.china08.yxyapi.api.manage.edu.platform.slider;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.platform.YxyHomeSlider;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeSliderRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 焦点图管理 搜索
 * 
 */
@CrossOrigin
@RestController
public class SliderUpdateOrderNumController extends YxyApiBaseController {

    @Autowired
    private YxyHomeSliderRepository yhsr;

    @ApiOperation(value = "教育局 焦点图列表")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/platform/slider/upd/ordernum", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        RespModel model = new RespModel();
        YxyHomeSlider currHomeSlider = yhsr.findByIdAndEdbId(reqModel.id, schoolId);
        Assert.notNull(currHomeSlider, "当前焦点图不存在，请刷新页面");
        YxyHomeSlider destHomeSlider = yhsr.findByEdbIdAndOrderNum(schoolId, reqModel.orderNum);
        if (destHomeSlider != null) {
            // 目标焦点图
            Integer origin = currHomeSlider.getOrderNum();
            Integer dest = destHomeSlider.getOrderNum();
            currHomeSlider.setOrderNum(dest);
            //
            destHomeSlider.setOrderNum(origin);
            yhsr.save(currHomeSlider);
            yhsr.save(destHomeSlider);
        } else {
            currHomeSlider.setOrderNum(reqModel.orderNum);
            yhsr.save(currHomeSlider);
        }
        model.setMessage("成功");
        return model;
    }

    protected static class ReqModel {

        public String id;

        public Integer orderNum;

    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String img;
        public String url;
        public Integer orderNum;
    }

}
